package com.xinli.xinlifm.view.fragment.forum;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.ForumBean;
import com.xinli.xinlifm.mvp.iview.MvpView;
import com.xinli.xinlifm.mvp.model.SuperModel;
import com.xinli.xinlifm.view.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by XiaoLiu on 2016/10/13.
 */

public class Froum_JingHuaFragment extends BaseFragment<SuperModel> implements MvpView<ForumBean>, SwipeRefreshLayout.OnRefreshListener  {
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private FroumFragmentAdapter froumFragmentAdapter;
    private List<Object> list = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;
    private boolean canLoadMore = true;
    int limit = 10;  //每次显示的数据条数  offset 是数据的偏移量
    private int offest = 0 ;
    private boolean flag = true;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    swipeRefreshLayout.setRefreshing(false);
                    break;
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate =null ;
        if(inflate ==null){
            Log.e("s", "类名==Froum_JingHuaFragment" + "方法名==onViewCreated=====:" + "4444444");
            inflate =  inflater.inflate(R.layout.froum_two_choose_item, container, false);
        }
        return inflate;
    }

    @Override
    public SuperModel getModel() {
        return new SuperModel();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Log.e("s", "类名==Froum_JingHuaFragment" + "方法名==onViewCreated=====:" + "ffffffe");
        recyclerView = ((RecyclerView) view.findViewById(R.id.forum_recyclerView));
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.forum_refresh);
        swipeRefreshLayout.setOnRefreshListener(this);
        linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        froumFragmentAdapter = new FroumFragmentAdapter(getContext(),list);
        recyclerView.setAdapter(froumFragmentAdapter);
        recyclerView.setOnScrollListener(new OnRecyclerScrollListener());   //上拉加载数据
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        getModel().getForumData(0, offest ,limit ,1);
    }

    @Subscribe
    @Override
    public void showView(ForumBean data) {
        if(offest != 0 || flag){
            flag = false;
            froumFragmentAdapter.addAll(data.getData());
        }
        Log.e("自定义标签", "类名==ForumFragment" + "方法名==showView=====:" + data);
    }

    @Override
    public void onDestroy() {
        Log.e("s", "类名==Froum_JingHuaFragment" + "方法名==onViewCreated=====:" + "ffffffe");
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


    //下拉刷新
    @Override
    public void onRefresh() {
     //   getModel().getForumData(0, 1 ,limit ,1);
     handler.sendEmptyMessageDelayed(100,1000);
    }

    public class OnRecyclerScrollListener extends RecyclerView.OnScrollListener {

        int lastVisibleItem = 0;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            Log.e("自定义标签", "类名==OnRecyclerScrollListener" + "方法名==onScrollStateChanged===crr==:" + newState);
            if (canLoadMore && froumFragmentAdapter != null && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem+1 ==

                    froumFragmentAdapter.getItemCount()) {
                //滚动到底部了，可以进行数据加载等操作
                Log.e("自定义标签", "类名==OnRecyclerScrollListener" + "方法名==onScrollStateChanged=====:" + "hahahaha");
                if(offest >= 70){
                   froumFragmentAdapter.setLastShow(2);
                    return;
                }
                offest = offest+limit;
                getModel().getForumData(0, offest ,limit ,1);
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    }
}
